package cn.yunhe.servlet;

import cn.yunhe.dao.IMerchantsAccount;

import cn.yunhe.entity.merchantsentity.MerchantsAccount;

import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 王羊
 */
@WebServlet("shangjia/MerchantsAccountServletSJMDL")
public class MerchantsAccountServletSJMDL extends HttpServlet {
    SqlSession sqlSession = null;
    IMerchantsAccount iMerchantsAccount = null;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        //获取输入的注册名和密码和手机号
        String a = request.getParameter("account");
        String b = request.getParameter("password");


        sqlSession = SqlSessionFactoryUtil.getSqlSession();
        HttpSession session=request.getSession();
        iMerchantsAccount = sqlSession.getMapper(IMerchantsAccount.class);


        Map map = new HashMap();
        map.put("account", a);



        int in = iMerchantsAccount.getAllMerchantsAccount(map);
        if (in == 0) {
            out.print("<script>alert('该用户不存在');</script>");
            //输入不正确 跳转至注册页面
            out.print("<script>location.href='login.html';</script>");

        } else {
            map.put("password", b);
            MerchantsAccount merchantsAccount = iMerchantsAccount.denglu(map);
            if (merchantsAccount == null) {
                out.print("<script>alert('输入密码错误');location.href='login.html';</script>");
            } else {
                session.setAttribute("mid",merchantsAccount.getMid());
                out.print("<script>alert('登陆成功');location.href='index.html';</script>");
            }


        }

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

